<template>
  <el-dialog
    :title="dialogForm.id ?'编辑部门' :'新增部门'"
    :visible="visible"
    width="50%"
    @close="onDialogClose"
  >
    <el-form ref="form" :model="dialogForm" :rules="addDeptsFormRules" label-width="80px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="dialogForm.name" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="dialogForm.code" placeholder="请输入部门编码" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <!-- <el-input v-model="dialogForm.manager" placeholder="请输入部门负责人" /> -->
        <el-select v-model="dialogForm.manager" placeholder="请选择" @focus="onManager">
          <el-option
            v-for="item in dialogManager"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="dialogForm.introduce" placeholder="请输入部门介绍" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('input',false) ">取 消</el-button>
      <el-button type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>

</template>

<script>
import {
  getDepartments,
  addDepartments,
  getDepartmentsById,
  editDepartmentsById
} from '@/api/departments'
import { getEmployessListSimple } from '@/api/employees.js'
export default {
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    // 部门校验
    const validDeptName = async(rule, value, callback) => {
      // debugger
      let flag = false
      if (this.dialogForm.id) {
        // 编辑
        const res = await getDepartments()
        const newArr = res.depts.filter(item => item.pid === this.node.pid && item.id !== this.node.id)
        flag = newArr.some(itme => itme.name === value)
      } else {
        // 新增
        if (!this.node.children) return callback()
        flag = this.node.children.some(item => item.name === value)
      }

      if (flag) {
        callback(new Error('部门已存在'))
      }
      callback()
    }
    // 部门编码校验
    const validDeptCode = async(rule, value, callback) => {
      const res = await getDepartments()
      let flag = false
      if (this.dialogForm.id) {
        const newArr = res.depts.filter(item => item.code !== this.dialogForm.code)
        flag = newArr.some(item => item.code === value)
      } else {
        flag = res.depts.some(item => item.code === value)
      }
      if (flag) {
        callback(new Error('编码已存在'))
      }
      callback()
    }
    return {
      dialogForm: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      addDeptsFormRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称1-10位', trigger: 'blur' },
          { validator: validDeptName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称1-10位', trigger: 'blur' },
          { validator: validDeptCode, trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称1-50位', trigger: 'blur' }
        ]
      },
      dialogManager: []
    }
  },

  created() {

  },

  methods: {
    // 关闭弹层
    onDialogClose() {
      this.$emit('input', false)
      // 关闭弹层后重置数据，回到初始值
      this.$refs.form.resetFields()
      this.dialogForm = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    // 获取员工简单列表
    async onManager() {
      const res = await getEmployessListSimple()
      console.log(res)
      this.dialogManager = res
    },
    // 点击确定，保存表单
    async onSave() {
      // 表单校验
      await this.$refs.form.validate()
      try {
        if (this.dialogForm.id) {
          await this.$refs.form.validate()
          editDepartmentsById(this.dialogForm)
          this.$message.success('修改成功')
          this.$emit('edit-success')
        } else {
          console.log('成功')
          // 请求的pid就是node的id
          this.dialogForm.pid = this.node.id
          // 发送请求添加部门
          await addDepartments(this.dialogForm)
          this.$message.success('添加成功')
          // 重新获取页面数据
          this.$emit('add-success')
        }
        // 关闭弹层
        this.onDialogClose()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取最新数据
    async getDeptsInfo(id) {
      console.log('信息', id)
      const res = await getDepartmentsById(id)
      this.dialogForm = res
    }
  }
}
</script>

<style scoped lang='less'>

</style>
